package com.kecheng.action;

import com.kecheng.com.utils.CommonUtil;
import com.kecheng.dao.CommonDao;
import com.kecheng.dto.FilterCondition;
import com.kecheng.dto.HotBanjiDto;
import com.kecheng.dto.WebCommonsResponse;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Banji;
import com.kecheng.pojo.FamilyTeach;
import com.kecheng.pojo.KeChengClassify;
import com.kecheng.pojo.Teacher;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 后台教师模块
 * @author
 */
@ParentPackage("json-default")
@Action(value = "familyTeach", results = {
        @Result(name = "modifyJn", type = "json",params = {"root","familyTeach"}),
        @Result(name = "webResponse", type = "json", params = {"root", "webCommonsResponse"})
})
@AllowedMethods(value = {("regex:.*")})
public class FamilyTeachAction extends BaseAction {
    @Autowired
    CommonDao dao;
    private FamilyTeach familyTeach;
    private Teacher teacher;
    private Banji banji;
    private FilterCondition filterCondition;

    //**************************************微信端代码****************************
    /**
     * 预约报班 详情
     * @return
     */
    public String banjiDetail() {
        HotBanjiDto hotBanjiDto = new HotBanjiDto();
        if(banji!=null){
            banji = (Banji)dao.findOne(banji);
            teacher = new Teacher();
            teacher.setId(banji.getTeacherId());
            teacher = (Teacher)dao.findOne(teacher);
            hotBanjiDto.setBanji(banji);
            hotBanjiDto.setTeacher(teacher);
        }

        webCommonsResponse = WebCommonsResponse.ok(hotBanjiDto);
        return "webResponse";
    }

    /**
     * 预约家教详情
     * @return
     */
    public String teacherDetail() {
        if(teacher!=null) teacher = (Teacher)dao.findOne(teacher);
        else{
            teacher = new Teacher();
        }
        webCommonsResponse = WebCommonsResponse.ok(teacher);
        return "webResponse";
    }


    /**
     *预约--二级分类列表
     * @return
     */
    public String yuClassify() {
        KeChengClassify classify = new KeChengClassify();
        classify.setLevel(1);
        List<KeChengClassify>keChengClassifyList = (List<KeChengClassify>) dao.find(classify);
        webCommonsResponse = WebCommonsResponse.ok(keChengClassifyList);
        return "webResponse";
    }

    /**
     * 预约--老师列表--家教
     * @return
     */
    public String yuTeacher(){
        StringBuilder sql = new StringBuilder("select t.* from teacher t " +
                "LEFT JOIN familyteach ft on t.id=ft.teacherId ");
        if (!com.kecheng.com.utils.CommonUtil.isEmpty(filterCondition)) {
            //筛选老师（二级课程分类id，所在区域，授课方式，老师性别，上课时间，教学经验，班级规模）
            if(CommonUtil.isEmpty(filterCondition.getClassifyId())){//课程id
                sql.append(" LEFT JOIN teacherskill ts on t.id=ts.teacherId " +
                        "where ft.isOpen =1 and ts.classifyId="+filterCondition.getClassifyId());
            }
            if(CommonUtil.isEmpty(filterCondition.getSex())){//教师性别
                sql.append(" and t.sex="+filterCondition.getSex());
            }
            if(CommonUtil.isEmpty(filterCondition.getTeachYears())){//教学年限
                String jxnx = filterCondition.getTeachYears();
                String min =jxnx .split("-")[0];
                String max = jxnx.split("-")[0];
                sql.append(" and t.teachAge>="+min +" and t.teachAge<="+max);
            }
        }else {
            sql.append(" where ft.isOpen = 1");//打开家教的所有教师
        }
        String countSql = sql.toString().replace("t.*","count(t.*)");
        PageBean<Teacher> teacherPageBean = (PageBean<Teacher>) dao.hqlFindPageBean(sql.toString(),countSql,filterCondition,Teacher.class);

        webCommonsResponse = WebCommonsResponse.ok(teacherPageBean);
        return "webResponse";
    }

    //######**************************************后端代码********************************************************
    /**
     * 查询家教参数
     * @return
     */
    public String findFaTeach(){
        familyTeach = (FamilyTeach)dao.findOne(familyTeach,"teacherId");
        return "modifyJn";
    }
    /**
     * 保存家教参数
     * @return
     */
    @Transactional(readOnly = false)
    public String saveFaTeach(){
        if(familyTeach.getId()==null){
            dao.add(familyTeach);
        }else{
            dao.update(familyTeach);
        }
        return "modifyJn";
    }

    public FamilyTeach getFamilyTeach() {
        return familyTeach;
    }

    public void setFamilyTeach(FamilyTeach familyTeach) {
        this.familyTeach = familyTeach;
    }

    public FilterCondition getFilterCondition() {
        return filterCondition;
    }

    public void setFilterCondition(FilterCondition filterCondition) {
        this.filterCondition = filterCondition;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Banji getBanji() {
        return banji;
    }

    public void setBanji(Banji banji) {
        this.banji = banji;
    }
}
